package com.code.petapp.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.SearchView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.code.petapp.adapter.SubmitImgAdapter;
import com.code.petapp.bean.SubmitBean;
import com.code.petapp.databinding.ActivitySubImgBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.SPCommonUtils;
import com.code.petapp.util.SubmitUtils;

import java.util.ArrayList;
import java.util.List;

public class SubImgActivity extends BaseActivity {
    ActivitySubImgBinding binding;
    private RecyclerView recyclerView;//声明RecyclerView
    private SubmitImgAdapter adapter1;
    private Context context;
    private List<SubmitBean> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySubImgBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        context = this;
        recyclerView = binding.recView;
        list = new ArrayList<>();
        load();
        search();
        adapter1=new SubmitImgAdapter(context,list);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter1);
        binding.faAdd.setOnClickListener(v -> {
            startActivity(new Intent(SubImgActivity.this,SubmitActivity.class));
            finish();
        });
    }
    private void search(){
        binding.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                searchData(s);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                searchData(s);
                return false;
            }
        });
    }
    private void searchData(String s){
        load(s);
    }
    private void load(){
        SubmitUtils.selectAll(SPCommonUtils.getInstance().getToken(), new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(ArrayList<SubmitBean> beanList) {
                list.clear();
                list.addAll(beanList);
                adapter1.notifyDataSetChanged();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }
    private void load(String desc) {
        SubmitUtils.selectDesc(SPCommonUtils.getInstance().getToken(),desc ,new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(ArrayList<SubmitBean> beanList) {
                list.clear();
                list.addAll(beanList);
                adapter1.notifyDataSetChanged();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }
}